
$(function(){
	$("select[name='keySurgeryCode']").each(function(){
		var options = "";
		options+="<option value='1'>1.甲状腺癌手术</option>";
		options+="<option value='2'>2.喉癌手术</option>";
		options+="<option value='3'>3.肺癌手术</option>";
		options+="<option value='4'>4.食管癌手术</option>";
		options+="<option value='5'>5.胃癌手术</option>";
		options+="<option value='6'>6.肝癌手术</option>";
		options+="<option value='7'>7.结直肠癌手术</option>";
		options+="<option value='8'>8.胰腺癌手术</option>";
		options+="<option value='9'>9.乳腺癌手术</option>";
		options+="<option value='10'>10.肾癌手术</option>";
		options+="<option value='11'>11.前列腺癌手术</option>";
		options+="<option value='12'>12.膀胱癌手术</option>";
		options+="<option value='13'>13.卵巢癌手术</option>";
		options+="<option value='14'>14.宫颈癌手术</option>";
		$(this).append(options);
	});
	var validator = $("#queryForm").validate({
		errorElement: "title",
		ignoreTitle: true,
		submitHandler : function(form){
			var actionUrl = $(form).attr("action");
			if(actionUrl){
				return true;
			}else{
				queryFormSubmit();
			}
		},
		rules:{
			outHospitalStartDate: {
				required: true
            },
            outHospitalEndDate: {
				required: true
            },
            keySurgeryCode: {
				required: true
            }
            
		},
		
	});
	
	
});


/*点击查询按钮*/
function queryBtnClick(){
	$("input[name='currentPage']").val(1);
	$("#queryForm").attr("action",null).submit();
}

/*提交查询*/
function queryFormSubmit(){
	var submitData = $('#queryForm').serialize();
	var basePath = $("#basePath").val();
	layer.load(1);
	$("#export_h").html("");
	clearPage();
	$.ajax({
		url: basePath + "statistics_analysis/tertiary_hospital_malignant_tumor",
		dataType: "json",
		data:submitData,
		success: function( data ) {
			var success = data['success'];
			if(success){
				var params = data['data'];
				
				var totalCounts = params['totalCounts'];
				if(totalCounts > 0){
					layer.msg("查询成功");
					var records = params['datas'];
					addRows(records);
					var pageSize = params['pageSize'];
					var currentPage = params['currentPage'];
					initPage(parseInt(totalCounts),parseInt(pageSize),parseInt(currentPage));
				}else{
					layer.msg("查询结果为空");
				}
				
			}else{
				layer.msg("查询失败");
			}
			console.log(data);
			
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			if(XMLHttpRequest.status == 200){
				if(textStatus == 'parsererror'){
					layer.msg("登陆已过期，请重新登陆");
				}else{
					layer.msg("未知错误，请联系管理员");
				}
			}else if(XMLHttpRequest.status == 0){
				if(textStatus == 'error'){
					layer.msg("无法连接服务器，请检测网络");
				}else{
					layer.msg("未知错误，请联系管理员");
				}
			}else{
				layer.msg("未知错误，请联系管理员");
			}
		},
		complete:function(XMLHttpRequest, textStatus){
			layer.closeAll('loading');
			parent.document.getElementById("mainContent").height=document.body.scrollHeight;
		}
	});
	
};

/*添加列表行*/
function addRows(records){
	var basePath = $("#basePath").val();
	for(var index in records){
		var record = records[index];
		if(record.surgery_count > 0){
			$("#export_h").html("<input type='button' value='导出' onclick='exportToExcel()'/>");
		}
		var add_content=$("#template_tr").clone();
		add_content.removeAttr("hidden");
		add_content.find("span[class='surgery_count']").html(record.surgery_count);
		add_content.find("span[class='death_count']").html(record.death_count);
		add_content.find("span[class='count_15']").html(record.count_15);
		add_content.find("span[class='avg_in_hospital_day']").html(record.avg_in_hospital_day);
		add_content.find("span[class='avg_in_hospital_cost']").html(record.avg_in_hospital_cost);
		$("#query_show_table tbody").append(add_content);
    } 
	
}

/*清空列表*/
function clearPage(){
	$("#template_tr").siblings("tr").remove();
	$("#page_plus").html("");
	$("#totalPage").html("0");
    $("#currentPage").html("0/0");
}

/*初始化分页控件*/
function initPage(totalCounts,pageSize,currentPage){
	 var visiblePages = 5;
	 var totalPages =  totalCounts%pageSize==0?(totalCounts/pageSize):(parseInt(totalCounts/pageSize)+1);
	 if(totalCounts < 1){
		$("#page_plus").html("");
		$("#totalPage").html("0");
        $("#currentPage").html("0/0");
	 }else{
		 $('#page_plus').jqPaginator({

	        totalCounts: totalCounts,
	        pageSize:pageSize,
	        visiblePages: visiblePages,
	        currentPage: currentPage,

	        first:'<li class="first"><a href="javascript:void(0);">首页</a></li>',
	        prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
	        next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
	        last: '<li class="last"><a href="javascript:void(0);">末页</a></li>',
	        page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
	        onPageChange: function (num,type) {

	     		if(type !='init'){
	     			$("input[name='currentPage']").val(num);
	     			queryFormSubmit();
				}
	            $("#totalPage").html(totalCounts);
	            $("#currentPage").html(num+"/"+totalPages);
	        }
	    });
	 }
}

function exportToExcel(){
	var basePath = $("#basePath").val();
	$("#queryForm").attr("action",basePath+"statistics_analysis/tertiary_hospital_malignant_tumor_export_excel");
	$("#queryForm").submit();
}